import PageBase from "../../../Game/Base/PageBase";
import { PAGE } from "../../../Game/Enum";
import mainRoot from "../../../Game/MainRoot";
import gameConfig from "../../../engines/configs/gameConfig";
import O1HD, { services } from "../../../engines/gameEngine";
import { CsvFileKind } from "../../../engines/libs/CsvFileKind";
import CsvDataManage from "../../../engines/managers/CsvDataManage";
import { REWARD_TYPE } from "../../../engines/services/userContent/userInterface";
import level_peopleArchery from "../level_peopleArchery";
import level_peopleArchery_resetInfinityPage from "./level_peopleArchery_resetInfinityPage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_peopleArchery_ResultPage extends PageBase {

    @property({type:cc.Label,tooltip:"击杀怪物数量文本"})
    lbKillCount:cc.Label = null;

    @property({type:cc.Label,tooltip:"造成伤害文本"})
    lbDammage:cc.Label = null;

    @property({type:cc.Label,tooltip:"时间文本"})
    lbTime:cc.Label = null;

    @property({type:cc.Node,tooltip:"失败界面"})
    loseLayout:cc.Node = null;

    @property({type:cc.Node,tooltip:"胜利界面"})
    winLayout:cc.Node = null;

    @property({type:cc.SpriteFrame,tooltip:"评级图片"})
    evaluateSps:cc.SpriteFrame[] = [];

    @property({type:cc.Sprite,tooltip:"评级图片"})
    evaluateSp:cc.Sprite = null;
    
    @property({type:cc.Sprite,tooltip:"光效节点"})
    lightSp:cc.Sprite = null;

    @property({type:cc.SpriteFrame,tooltip:"光效图片"})
    lightSps:cc.SpriteFrame[] = [];

    @property({type:cc.Node,tooltip:"重置按钮"})
    resetBtn:cc.Node = null;

    level:level_peopleArchery = null;

    winFlag:boolean = false;

    bindData(level:level_peopleArchery,winFlag:boolean){
        this.level = level;
        this.winFlag = winFlag;
        this.refrenshPage();
        this.resetBtn.active = false;
        // if(O1HD.getManagers().peopleArcheryManager.getData().infinityLevel >= Object.keys((O1HD.getManagers().csvDataManager.getCsvData(CsvFileKind.CsvFile.peopleArcheryInfinityEnemyHp)["title"])).length - 1){
        //     this.resetBtn.active = true;
        // }else if(!this.winFlag && this.level.getLevelInfo().infinityFlag){
        //     this.resetBtn.active = true;
        // }
        this.node.zIndex = cc.macro.MAX_ZINDEX;
    }

    refrenshPage(){
        this.lbKillCount.string = this.level.killCount.toString();
        this.lbDammage.string = Math.floor(this.level.damage).toString();
        this.lbTime.string = Math.floor(this.level.gameTime / 60) + "分" + Math.floor(this.level.gameTime % 60) + "秒";
        let wave:number = this.level.realWave + 1;
        let index:number = 0;
        if(wave <= 3){
            index = 0;
        }else if(wave <= 10){
            index = 1;
        }else if(wave <= 15){
            index = 2;
        }else if(wave <= 20){
            index = 3;
        }else if(wave <= 30){
            index = 4;
        }else if(wave <= 40){
            index = 5;
        }else{
            index = 6;
        }
        this.evaluateSp.spriteFrame = this.evaluateSps[index];
        this.winLayout.active = this.winFlag;
        this.loseLayout.active = !this.winFlag;
        this.evaluateSp.node.scale = 1.5;
        cc.tween(this.evaluateSp.node).to(0.2,{scale:1},{easing:cc.easing.backOut}).start();
        this.lightSp.spriteFrame = this.winFlag ? this.lightSps[0] : this.lightSps[1];
        this.lightAni();
        // if(this.winFlag){
            // let moneyNum:number = Number(this.level.peopleArcheryData["complete_money"]);
            // let addition:number = moneyNum * this.level.role.goldAddition;
            // O1HD.getManagers().peopleArcheryManager.setData({money:moneyNum + addition},true);
        // }
    }

    lightAni(){
        let tween = cc.tween(this.lightSp.node).by(3,{angle:360});
        cc.tween(this.lightSp.node).repeatForever(tween).start();
    }

    async onBtnBackMain(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        await O1HD.getManagers().gameMgr.returnMainScene();
        mainRoot.UISystem.CloseAllPage();
        
        mainRoot.UISystem.OpenPage(PAGE.PEOPLE_ARCHERY_START_PAGE);
    }

    onBtnAlive(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        if(O1HD.getServices().userSrv.getCoins() > 0){
            O1HD.getServices().userSrv.setCoins(-1,true);
            this.level.peopleAlive();
            this.closeThis();
        }else{
            O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                this.level.peopleAlive();
                this.closeThis();
            },()=>{

            },null,"看广告复活");
        }
    }

    async onBtnAgain(){
        let needPower:number = this.level.getLevelInfo()["subPower"] || 1;
        if(O1HD.getServices().userSrv.getPower() - needPower < 0){
            mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
            return;
        }
        services().userSrv.setPower(-needPower,true,true);
        // mainRoot.LevelSystem.AddLevel(this.m_mode, -1);
        await mainRoot.UISystem.ClosePage(PAGE.PEOPLE_ARCHERY_INGAME_PAGE);
        await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode,null,true,this.level.getLevelInfo().infinityFlag? this.level.getLevelInfo() : null);
        this.closeThis();
    }

    async onBtnResetInfinity(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");  
        let page = await mainRoot.UISystem.OpenPage(PAGE.LEVEL_PEOPLE_ARCHERY_RESETINFINITYPAGE);
        (page as level_peopleArchery_resetInfinityPage).bindPage(this);
    }

    async onResetInfinity(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");        
        let needPower:number = this.level.getLevelInfo()["subPower"] || 1;
        if(O1HD.getServices().userSrv.getPower() - needPower < 0){
            mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
            return;
        }
        services().userSrv.setPower(-needPower,true,true);
        O1HD.getManagers().peopleArcheryManager.setData({curInfinityLevel:0},false);
        await mainRoot.UISystem.CloseAllPage();
        await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode,null,true,this.level.getLevelInfo().infinityFlag? this.level.getLevelInfo() : null);
    }

    async onBtnNext(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        let needPower:number = this.level.getLevelInfo()["subPower"] || 1;
        if(O1HD.getServices().userSrv.getPower() - needPower < 0){
            mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
            return;
        }
        services().userSrv.setPower(-needPower,true,true);
        await mainRoot.UISystem.ClosePage(PAGE.PEOPLE_ARCHERY_INGAME_PAGE);
        if(this.level.getLevelInfo()["infinityFlag"]){

        }
        await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode,null,true,this.level.getLevelInfo().infinityFlag ? this.level.getLevelInfo() : null);
        this.closeThis();
    }

    async closeThis(){
        super.ClosePage();
    }
    
}
